<template>
	<view>
		<view class="goods-list">
			<!-- 商品左侧图片区域 -->
			<!-- 商品右侧信息区域 -->
			<!-- 商品标题 -->
			<!-- 商品价格 -->
			<!-- <block v-for="(goods,i) in goodsList" :key="i">
				<view class="goods-item">
					<view class="goods-item-left">
						<image :src="goods.goods_small_logo || defaultPic" class="goods-pic"></image>
					</view>
					<view class="goods-item-right">
						<view class="goods-name">{{goods.goods_name}}</view>
						<view class="goods-info-box">
							<view class="goods-price">¥{{goods.goods_price}}</view>
						</view>
						
					</view>
				</view>
			</block> -->
			<!-- 为 my-goods 组件动态绑定 goods 属性的值 -->
			<!-- 优化：在goods_list组件中，循环渲染my-goods组件即可： -->
			<view v-for="(item,i) in goodsList" :key="i" @click="gotoDetail(item)">
				<my-goods :goods="item"></my-goods>
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				//请求参数对象
				queryObj: {
					//查询关键词
					query: '',
					//商品分类id
					cid: '',
					pagenum: 1,
					pageSize: 10
				},
				//商品列表的数据
				goodsList: [],
				//总条数
				total: 0,
				defaultPic: '',
				isLoading: false

			};
		},
		onLoad(options) {
			//options.query=applequery=apple
			this.queryObj.query = options.query || ''
			this.queryObj.cid = options.cid || ''
			//调用获取商品列表数据的方法
			this.getGoodsList()

		},
		onReachBottom() {
			//判断是否还有下一页数据
			if (this.queryObj.pagenum * this.queryObj.pageSize >= this.total) return uni.$showMsg('数据加载完毕！')
			//判断是否正在请求其它数据，如果是，则不发起额外的请求
			if (this.isLoading) return
			this.queryObj.pagenum += 1
			this.getGoodsList()
		},
		onPullDownRefresh() {
			//1.重置关键数据
			this.queryObj.pagenum = 1
			this.total = 0
			this.isLoading = false
			this.goodsList = []
			//2.重新发起请求
			this.getGoodsList(() => uni.stopPullDownRefresh())
		},
		methods: {
			async getGoodsList(cb) {
				//在请求数据前后分别打开和关闭节流阀
				//打开节流阀
				this.isLoading = true
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
				//关闭节流阀
				this.isLoading = false
				//只要数据加载完毕，就立即按需调用cb回调函数
				cb && cb()
				if (res.meta.status !== 200) return uni.$showMsg()
				// this.goodsList = res.message.goods
				//优化下拉没数据变空拉不回来或者只能展示固定条数 通过展开运算符进行新旧数据的拼接
				this.goodsList = [...this.goodsList, ...res.message.goods]
				this.total = res.message.total
			},
			gotoDetail(item){
				uni.navigateTo({
					url:'/subPkg/goods_detail/goods_detail?goods_id='+item.goods_id
				})
				
			}
		}
	}
</script>

<style lang="scss">
	.goods-item {
		display: flex;
		padding: 10px 5px;
		border-bottom: 1px solid #f0f0f0;

		.goods-item-left {
			margin-right: 5px;

			.goods-pic {
				width: 100px;
				height: 100px;
				display: block;
			}
		}

		.goods-item-right {
			display: flex;
			flex-direction: column;
			justify-content: space-between;

			.goods-name {
				font-size: 13px;
			}

			.goods-price {
				font-size: 16px;
				color: #c00000;
			}
		}
	}
</style>